package com.redwine.app;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.net.GsonRequest;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.BaseResponse;
import com.redwine.app.net.responses.SendMsgEvent;
import com.redwine.app.utils.ActivitySplitAnimationUtil;
import com.redwine.app.utils.Global;
import com.redwine.app.utils.NetUtil;
import com.redwine.app.utils.Settings;
import com.redwine.app.utils.Utils;

import java.util.LinkedHashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.redwine.app.utils.Utils.showToast;

/**
 * 修改密码页面
 * 作者：gassion on 17/9/5 22:18
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class UpdatePasswordActivity extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    @ViewInject(R.id.et_old_password)
    EditText tvOldPassword;
    @ViewInject(R.id.et_new_password)
    EditText tvNewPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        headerTitle.setText(R.string.update_password);
        layoutBack.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.layout_back, R.id.tv_confirm})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;

            case R.id.tv_confirm:
                submit();
                break;
        }
    }


    private void submit() {

        String oldPassword = tvOldPassword.getText().toString().trim();
        if (TextUtils.isEmpty(oldPassword)) {
            Toast.makeText(this, "旧密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String newPassword = tvNewPassword.getText().toString().trim();
        if (TextUtils.isEmpty(newPassword)) {
            Toast.makeText(this, "新密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (oldPassword.length() < 6) {
            Toast.makeText(this, "旧密码格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }

        if (newPassword.length() < 6) {
            Toast.makeText(this, "新密码格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }

        requestChangePasswordData(oldPassword, newPassword);
    }


    /**
     * 修改密码
     * 作者：gassion on 17/8/26 16:15
     * 最可怕的敌人，就是没有坚强的信念。
     */
    protected void requestChangePasswordData(String oldPassword, String newPassword) {
        if (!NetUtil.checkNet(this)) {
            showToast(R.string.generic_check);
            return;
        }
        showWaiting();
        String url = NetworkConstants.CHANGE_PASSWORD_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("uid", Global.user.getUid());
        params.put("old_password", oldPassword);
        params.put("new_password", newPassword);
        GsonRequest<BaseResponse> request = new GsonRequest<>(url, Request.Method.POST,
                BaseResponse.class, false, params, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse response) {
                dismissWaiting();
                Utils.showToast(response.getMsg());
                if (response.isSuccess()) {
                    Settings.logOut();
                    EventBus.getDefault().post(new SendMsgEvent(SendMsgEvent.EXIT_ACCOUNT));
                    Intent intent = new Intent(UpdatePasswordActivity.this, LoginActivity.class);
                    ActivitySplitAnimationUtil.startActivity(UpdatePasswordActivity.this, intent);
                    finish();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                showToast(error.getMessage());
                dismissWaiting();
            }
        });
        APP.getInstance().getRequestQueue().add(request);
    }

}
